#!/bin/sh
set -e

release=$1
dist=$2
architecture=$3
tag=release_`echo $release | sed 's/\./_/g'`

if [ -d fink ] && [ ! -d fink/CVS ]; then
  echo "Removing fink/ (no CVS dir found)..."
  rm -r fink
fi
if [ ! -d fink ]; then
  echo "Checking out fink/ from CVS..."
  cvs -z3 -d:pserver:anonymous@fink.cvs.sourceforge.net:/cvsroot/fink co -d fink dists
fi
cd fink
echo "Updating from CVS (Tag $tag)..."
cvs -z3 -q update -r $tag
cd ..

echo "Linking fink/dists to $dist..."
rm -f fink/dists var/db/fink.db
ln -s $dist fink/dists

# As of 0.6.3 / 0.7.0.. you need to specify the dist you want 'stable' to be. 
# That is, './dumprelease 0.6.3 10.2-gcc3.3' and './dumprelease 0.7.0 10.3'
# This may be temporary till we find a better solution.

# As of 0.8.1, you also need to specify the architecture (either i386
# or powerpc), and the output filename will contain the architecture string.  
# That is, './dumprelease 0.8.1 10.4 i386' will produce 
# $release-$architecture-stable.sql
if [ -z "$architecture" ]; then
 echo "Dumping $dist/stable to $release-stable.sql..."
 ./dump --distribution=$dist --release=$release --architecture=powerpc >$release-stable.sql
else
 echo "Dumping $dist/stable to $release-$architecture-stable.sql..."
 ./dump --distribution=$dist --release=$release --architecture=$architecture >$release-$architecture-stable.sql
fi
if [ -f var/db/fink.db ]; then
 rm var/db/fink.db
fi

exit 0
